package com.xinxin.facelinker.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chatuidemo.R;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xinxin.facelinker.Config;
import com.xinxin.facelinker.domain.ReceiveNewPalInfo;
import com.xinxin.facelinker.domain.ReceiveNewPals;
import com.xinxin.facelinker.ui.button.views.ButtonRectangle;
import com.xinxin.facelinker.utils.NetHelper;

public class ReceiveNewPalInfoActivity extends Activity {

    private TextView tvAccountNum;
    private TextView tvGender;
    private TextView tvMessage;
    private ButtonRectangle btnAccept;
    private ButtonRectangle btnRefuse;
    private HttpUtils httpUtils = new HttpUtils();
    private View btnBack;
    private ImageView ivPhoto;
    private ImageLoader imageLoader=ImageLoader.getInstance();
    private String my_account_num;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive_new_pal_info);

        tvAccountNum = (TextView) findViewById(R.id.tvAccountNum);
        tvGender = (TextView) findViewById(R.id.tvGender);
        tvMessage = (TextView) findViewById(R.id.tvMessage);
        btnAccept = (ButtonRectangle) findViewById(R.id.btnAccept);
        btnRefuse = (ButtonRectangle) findViewById(R.id.btnRefuse);
        btnBack = findViewById(R.id.btnBack);
        ivPhoto= (ImageView) findViewById(R.id.ivPhoto);
        my_account_num=getIntent().getStringExtra(Config.KEY_MY_ACCOUNT_NUM);

        tvAccountNum.setText(getIntent().getStringExtra(Config.KEY_OTHER_ACCOUNT_NUM));
        tvGender.setText(getIntent().getStringExtra(Config.KEY_GENDER));
        tvMessage.setText(getIntent().getStringExtra(Config.KEY_RECEIVE_NEW_PAL_MESSAGE));
        imageLoader.displayImage(getIntent().getStringExtra(Config.KEY_PHOTO_URL),ivPhoto);

        //接受按钮
        btnAccept.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RequestParams params = new RequestParams();
                params.addBodyParameter(Config.ACTION, Config.ACTION_ACCEPT_ADD_NEW_PAL);
                params.addBodyParameter(Config.KEY_MY_ACCOUNT_NUM, my_account_num);
                params.addBodyParameter(Config.KEY_OTHER_ACCOUNT_NUM, getIntent().getStringExtra(Config.KEY_OTHER_ACCOUNT_NUM));
                httpUtils.send(HttpRequest.HttpMethod.POST, Config.CATEGORIES_URL, params, new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        Toast.makeText(ReceiveNewPalInfoActivity.this, R.string.answer_new_pal_success, Toast.LENGTH_SHORT).show();
                        finish();
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        Toast.makeText(ReceiveNewPalInfoActivity.this, R.string.answer_new_pal_fail, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

        //拒绝按钮
        btnRefuse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RequestParams params = new RequestParams();
                params.addBodyParameter(Config.ACTION, Config.ACTION_REFUSE_ADD_NEW_PAL);
                params.addBodyParameter(Config.KEY_MY_ACCOUNT_NUM, my_account_num);
                params.addBodyParameter(Config.KEY_OTHER_ACCOUNT_NUM, getIntent().getStringExtra(Config.KEY_OTHER_ACCOUNT_NUM));
                httpUtils.send(HttpRequest.HttpMethod.POST, Config.CATEGORIES_URL, params, new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        Toast.makeText(ReceiveNewPalInfoActivity.this, R.string.answer_new_pal_success, Toast.LENGTH_SHORT).show();
                        finish();
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        Toast.makeText(ReceiveNewPalInfoActivity.this, R.string.answer_new_pal_fail, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }

        return true;
    }
}
